package com.my.htmlpdf;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import com.lowagie.text.pdf.BaseFont;



public class HtmltoPDF {

	public static void main(String[] args) throws Exception {
		// htmlToPdf();
		// htmlToPdf2();
		htmlToPdf3();
	}


	public static void htmlToPdf3() throws Exception {
		
		String inputFile = "D:\\project\\simple_activiti\\src\\main\\webapp\\WEB-INF\\pages\\html-pdf.html";
		String outFile = "D:\\project\\simple_activiti\\src\\main\\webapp\\WEB-INF\\pages\\html-pdf.pdf";
		OutputStream os = null;
		os = new FileOutputStream(outFile);
		ITextRenderer renderer = new ITextRenderer();
		ITextFontResolver fontResolver = renderer.getFontResolver();
		fontResolver.addFont("C:/Windows/fonts/simsunb.ttf",
				BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

		String url = new File(inputFile).toURI().toURL().toString();
		System.out.println("=============url: " + url);
		renderer.setDocument(url);
		renderer.layout();
		renderer.createPDF(os,true);
		System.out.println("======转换成功!");
		os.close();
	}

}